package com.tju.backend.utils.modbus_utils;

import com.tju.backend.utils.hex.HexStringUtil;
import com.tju.backend.utils.tool.Constants;
import com.tju.backend.utils.tool.EmptyUtils;
import lombok.extern.slf4j.Slf4j;

/**
 * 代码拼接工具
 *
 * @author tju
 * @CREATE 2021 /05 /18 12:40
 */
@Slf4j
public class MySplicingCode {

    //写字符
    public static String writeCharacter(MoWrite moWrite, String msg) throws Exception {
        if (EmptyUtils.isNotEmpty(moWrite.getSite())) {
            //1.报文
            String message = Constants.MESSAGE_ONE;
            //2.处理起始地址
//            String[] initialAddressGroup = info.getRRegisterAddress().split("_");
            int s = (Integer.parseInt(moWrite.getSite()) - 1);
            String initialAddress = String.valueOf(s);
            if (initialAddress.length() > 1) {
                initialAddress = Long.toHexString(Long.parseLong(initialAddress));
            } else {
                initialAddress = Long.toHexString(Long.parseLong(moWrite.getSite()));
            }
            if (!(initialAddress.length() % 2 == 0)) {
                initialAddress = "0" + initialAddress;
            } else
                initialAddress = "00" + initialAddress;
            //3.寄存器的个数和字符的长度
            String numberLength = Constants.NUMBER_LENGTH;
            //4.处理拼接的字符串数据
            String data = zeroPadding((120 - msg.length()));
            data = HexStringUtil.changeStringToHex(msg) + data;

            return message + initialAddress + numberLength + data;
        } else
            throw new NullPointerException("ModBus TCP代码地址为空");
    }


    //填充零
    public static String zeroPadding(int sum) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < sum; i++) {
            sb.append("00");
        }
        return sb.toString();
    }

    /**
     * 向Modbus TCP/IP写入开关量
     *
     * @param msg
     * @return
     */
    public static String lockSwitch(String initialAddress, String msg) {
        if (EmptyUtils.isNotEmpty(initialAddress)) {
            //1.报文
            String message = Constants.MESSAGE_TWO;
            //2.起始地址
            String address = null;
            if (initialAddress.length() % 2 == 0) {
                address = "00" + Integer.toHexString(Integer.parseInt(initialAddress) - 1);
            } else {
                address = "000" + Integer.toHexString(Integer.parseInt(initialAddress) - 1);
            }
            //3.处理开关量数据
            String data = msg + "00";
            return message + address + data;
        } else
            throw new NullPointerException("ModBus TCP代码地址为空");
    }

    /**
     * 向Modbus TCP/IP读取开关量
     *
     * @param info
     * @return
     */
//    public static String readSwitch(SysRegisterFacilityInfo info) {
//        if (EmptyUtils.isNotEmpty(info.getRRegisterAddress())) {
//            //1.报文
//            String message = com.backend.utils.Constants.MESSAGE_THREE;
//            //2.起始地址
//            String address = "00" + Integer.toHexString(Integer.parseInt(info.getRRegisterAddress()) - 1);
//            //3.读取1个开关量
//            String data = "0001";
//            return message + address + data;
//        } else
//            throw new NullPointerException("ModBus TCP代码地址为空");
//    }

}
